

local print_r = LuaPanda.tools.printTable

function table.merge(dst, src)
    for k, v in pairs(src) do
        dst[k] = v
    end
end

---测试弱表
local function test_weakTable()
    local weakTable = {}
    setmetatable(weakTable, {__mode = "v"})

    local a = {1}
    local b = {2}

    weakTable[#weakTable+1] = a
    weakTable[#weakTable+1] = b

    a = nil

    -- for k, v in pairs(tb) do
    --     if v[1] == 3 then
    --         tb[k] = nil
    --         break
    --     end
    -- end

    --垃圾回收
    collectgarbage()

    for k, v in pairs(weakTable) do
        print(k, v[1])
    end
end

---测试merge函数
local function test_tableMerge()
    local ta = {
        [2] = 2,
        a = 1,
        b = {
            "bb",
            "cc"
        }
    }
    local tb = {
        [2] = nil,
        a = 1,
        b = {
            "ab",
            "ac"
        }
    }

    print_r(ta)
    
    print("---line---")

    print_r(tb)

    table.merge(ta, tb)

    print("---line---")
    print_r(ta)
end

local function test_keyof()
    local a = "小白"
    local tList = {
        "小白",
        "大白",
        "葫芦"
    }
end

local function test_equal()
    local a = {1, 2}
    local b = {1, 2}

    local c = {"abcde", 123456}
    local d = {"abcde", 123456}

    local function isEqual(tb1, tb2)
        local map = {}
        for k, v in pairs(tb1) do
            map[v] = 1
        end
        for k, v in pairs(tb2) do
            if not map[v] then
                return false
            else
                map[v] = nil
            end
        end

        -- local ret = 0
        -- for k, v in pairs(map) do
        --     ret = map + 1
        -- end
        -- return ret == 0

        return (next(map) == nil)
    end

    print(a == b)
    print(isEqual(a, b))
    print(isEqual(c, d))
end

local function test_index()
    local tb = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    for k, v in pairs(tb) do
        print(k, v)
    end

    print("----------")

    local tbk = {
        [1] = 1,
        [2] = 2,
        [3] = 3,
        -- [4] = 4,
        -- [5] = 5,
        -- [6] = 6,
        -- [7] = 7,
        -- [8] = 8,
        -- [9] = 9,
        -- [10] = 10,
        -- [11] = 11,
    }
    for k, v in pairs(tbk) do
        print(k, v)
    end
end

local function test_insert()
    local t1 = {1, 2}

    local lessNum = 5 - #t1
    if lessNum > 0 then
        for i = 1, lessNum, 1 do
            table.insert(t1, t1[i])
        end
    end

    for k, v in pairs(t1) do
        print("key=" .. k, " val=" .. v)
    end
end

local function test_err()
    local tb1 = {}
    print(tb1.cfg)

    local tb2
    print(tb2.cfg)
end

local function test_remove()
    -- local tb = {1, 2, 3}
    local tb = setmetatable({1, 2, 3}, {__mode = "kv"})
    print(#tb)
    for i = #tb, 1, -1 do
        table.remove(tb, i)
    end

    collectgarbage('collect')

    print(#tb)
end

return function()
    print("===>Test: lua_table")
    -- test_weakTable()
    -- test_tableMerge()
    -- test_equal()
    test_index()
    -- test_insert()
    -- test_err()
    -- test_remove()
end
